<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.processInstanceId"
          clearable
          size="small"
          placeholder="流程实例ID"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.processDefinitionName"
          clearable
          size="small"
          placeholder="流程定义名称"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation>
        <template slot="left">
          <el-button
            v-permission="['admin', 'task:edit']"
            :disabled="!selectIndex"
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-upload"
            @click="crud.toEdit(currentRow)"
          >查看
          </el-button>
        </template>
      </crudOperation>
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="handleCurrentChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="processInstanceName" label="流程实例名称" />
      <el-table-column prop="name" label="任务名称" />
      <el-table-column prop="claimTime" label="开始时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="endTime" label="结束时间">
        <template slot-scope="scope">
          <span>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="ownerName" label="所有人" />
      <el-table-column prop="assigneeName" label="执行人" />
      <el-table-column
        v-permission="['admin', 'task:edit', 'task:del']"
        label="操作"
        width="200px"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="['admin', 'task:edit']"
            style="margin-left: 3px"
            type="text"
            size="mini"
            @click="info(scope.row.processInstanceId)"
          >详情
          </el-button>
          <el-button
            v-if="
              scope.row.endTime === null &&
                (scope.row.assignee === null || scope.row.assignee === '')
            "
            v-permission="['admin', 'task:edit']"
            style="margin-left: 3px"
            type="text"
            size="mini"
            @click="claim(scope.row)"
          >认领
          </el-button>
          <el-button
            v-if="
              scope.row.endTime === null &&
                scope.row.assignee === $store.getters.user.id + ''
            "
            v-permission="['admin', 'task:edit']"
            style="margin-left: 3px"
            type="text"
            size="mini"
            @click="unclaim(scope.row)"
          >取消认领
          </el-button>
          <el-button
            v-if="
              scope.row.endTime === null &&
                scope.row.assignee !== null &&
                scope.row.assignee === $store.getters.user.id + ''
            "
            v-permission="['admin', 'task:edit']"
            style="margin-left: 3px"
            type="text"
            size="mini"
            @click="execute(scope.row)"
          >执行
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--表单组件-->
    <el-dialog
      append-to-body
      :loading="loading"
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="800px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        size="small"
        label-width="100px"
        disabled
      >
        <el-form-item label="任务ID" prop="id">
          <el-input v-model="form.id" style="width: 220px" />
        </el-form-item>
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name" style="width: 220px" />
        </el-form-item>
        <el-form-item label="任务分类" prop="category">
          <el-input v-model="form.category" style="width: 220px" />
        </el-form-item>
        <el-form-item label="任务描述" prop="description">
          <el-input v-model="form.description" style="width: 220px" />
        </el-form-item>
        <el-form-item label="所有人" prop="owner">
          <el-input v-model="form.owner" style="width: 220px" />
        </el-form-item>
        <el-form-item label="所有人姓名" prop="ownerName">
          <el-input v-model="form.ownerName" style="width: 220px" />
        </el-form-item>
        <el-form-item label="认领时间" prop="claimTime">
          <el-date-picker
            v-model="form.claimTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 220px"
          />
        </el-form-item>
        <el-form-item label="执行人" prop="assignee">
          <el-input v-model="form.startUserId" style="width: 220px" />
        </el-form-item>
        <el-form-item label="执行人姓名" prop="assigneeName">
          <el-input v-model="form.durationInMillis" style="width: 220px" />
        </el-form-item>
        <el-form-item label="到期日" prop="dueDate">
          <el-date-picker
            v-model="form.dueDate"
            type="datetime"
            value-format="yyyy-MM-dd"
            style="width: 220px"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 220px"
          />
        </el-form-item>
        <el-form-item label="状态" prop="suspended">
          <el-input
            v-if="form.endTime == null"
            v-model="form.suspended ? '挂起' : '激活'"
            style="width: 220px"
          />
          <el-input v-else value="已完成" style="width: 220px" />
        </el-form-item>
        <el-form-item label="持续时间" prop="durationInMillis">
          <el-input v-model="form.durationInMillis" style="width: 220px" />
        </el-form-item>
        <el-form-item label="工作时间" prop="workTimeInMillis">
          <el-input v-model="form.workTimeInMillis" style="width: 220px" />
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-input v-model="form.priority" style="width: 220px" />
        </el-form-item>
        <el-form-item label="任务定义KEY" prop="taskDefinitionKey">
          <el-input v-model="form.taskDefinitionKey" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程实例ID" prop="processInstanceId">
          <el-input v-model="form.processInstanceId" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程实例名称" prop="processInstanceName">
          <el-input v-model="form.processInstanceName" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程定义ID" prop="processDefinitionId">
          <el-input v-model="form.processDefinitionId" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程定义名称" prop="processDefinitionName">
          <el-input v-model="form.processDefinitionName" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程定义描述" prop="processDefinitionDescription">
          <el-input v-model="form.processDefinitionDescription" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程定义KEY" prop="processDefinitionKey">
          <el-input v-model="form.processDefinitionKey" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程定义分类" prop="processDefinitionCategory">
          <el-input v-model="form.processDefinitionCategory" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程定义版本" prop="processDefinitionVersion">
          <el-input v-model="form.processDefinitionVersion" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程定义发布ID" prop="processDefinitionDeploymentId">
          <el-input v-model="form.processDefinitionDeploymentId" style="width: 220px" />
        </el-form-item>
        <el-form-item label="表单KEY" prop="formKey">
          <el-input v-model="form.formKey" style="width: 220px" />
        </el-form-item>
        <el-form-item label="流程启动人" prop="processInstanceStartUserId">
          <el-input v-model="form.processInstanceStartUserId" style="width: 220px" />
        </el-form-item>
        <el-form-item label="发起人完成任务" prop="initiatorCanCompleteTask">
          <el-select v-model="form.initiatorCanCompleteTask" style="width: 220px">
            <el-option
              v-for="item in trueOrFalse"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="候选组" prop="memberOfCandidateGroup">
          <el-select v-model="form.memberOfCandidateGroup" style="width: 220px">
            <el-option
              v-for="item in trueOrFalse"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="候选成员" prop="memberOfCandidateUsers">
          <el-select v-model="form.memberOfCandidateUsers" style="width: 220px">
            <el-option
              v-for="item in trueOrFalse"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="委派状态" prop="delegationState">
          <el-input v-model="form.delegationState" style="width: 220px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="crud.cancelCU">关闭</el-button>
      </div>
    </el-dialog>
    <!--详情-->
    <process-detail
      v-if="dialogViewVisible"
      :visible.sync="dialogViewVisible"
      :process-instance-id="processInstanceId"
    />
    <!-- 执行任务 -->
    <execute-task
      v-if="dialogExecuteTaskVisible"
      :visible.sync="dialogExecuteTaskVisible"
      :execute-task-id="executeTaskId"
      @executeTaskFinished="crud.toQuery()"
    />
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudTask from '@/api/flowable/task'
import processDetail from '../components/ProcessDetail'
import executeTask from '../components/ExecuteTask'

const defaultForm = {}
export default {
  name: 'FlowableTask',
  components: { executeTask, processDetail, pagination, crudOperation, rrOperation },
  cruds() {
    return CRUD({ title: '流程实例', url: 'api/flowable/task', crudMethod: { ...crudTask }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      loading: false,
      dialogViewVisible: false,
      dialogExecuteTaskVisible: false,
      processInstanceId: '',
      executeTaskId: '',
      selectIndex: '',
      currentRow: null,
      trueOrFalse: [
        { id: 1, label: '是', value: 'true' },
        { id: 2, label: '否', value: 'false' }
      ],
      permission: {
        add: ['admin', 'task:add'],
        edit: ['admin', 'task:edit'],
        del: ['admin', 'task:del']
      }
    }
  },
  created() {
    this.crud.optShow = {
      add: false,
      edit: false,
      del: false,
      download: false
    }
    if (this.$route.query && this.$route.query.processInstanceId) {
      this.crud.processInstanceId = this.$route.query.processInstanceId
    }
  },
  methods: {
    cancel() {
      this.dialog = false
    },
    // 编辑前
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.loading = true
      crudTask.getDetail(form.id).then(res => {
        this.form = res
        this.loading = false
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    // 详情
    info(processInstanceId) {
      this.processInstanceId = processInstanceId
      this.dialogViewVisible = true
    },
    // 认领
    claim(row) {
      crudTask.claim(row).then(res => {
        this.crud.dleChangePage(1)
        this.crud.notify('认领成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    // 取消认领
    unclaim(row) {
      crudTask.unclaim(row).then(res => {
        this.crud.dleChangePage(1)
        this.crud.notify('取消认领成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    // 执行
    execute(row) {
      this.executeTaskId = row.id
      this.dialogExecuteTaskVisible = true
    },
    handleCurrentChange(selection) {
      this.crud.selections = selection
      if (selection.length === 1) {
        const row = selection[0]
        this.selectIndex = row.id
        this.currentRow = row
      } else {
        this.currentRow = {}
        this.selectIndex = ''
      }
    }
  }
}
</script>
